package com.zhihuichongcheng.application.ui.im.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.chat.EMGroup;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;
import com.zhihuichongcheng.application.im.Constant;
import com.zhihuichongcheng.application.ui.im.activity.ChatActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/10/27.
 */

public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.ViewHold> {
    private List<EMGroup> mListData = new ArrayList<>();
    private Context mContext;

    private View.OnClickListener onClickListener;
    public GroupAdapter(Context context)
    {
        this.mContext = context;
    }

    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_group_contact,null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(final ViewHold holder, final int position) {
        Glide.with(mContext).load(R.drawable.ic_chat_group_defalut).transform(new GlideCircleTransform(mContext)).into(holder.mImAvatar);
        holder.mTvTitle.setText(mListData.get(position).getGroupName());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EMGroup emGroup = mListData.get(position);
                Intent intent = new Intent(mContext,ChatActivity.class);
                intent.putExtra(Constant.EXTRA_CHAT_TYPE, Constant.CHATTYPE_GROUP);
                intent.putExtra("userId",emGroup.getGroupId());
               mContext.startActivity(intent);
                ((Activity)mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void setData(List<EMGroup> list) {
        this.mListData = list;
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private ImageView mImAvatar;
        private View view;
        private TextView mTvTitle;
        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mImAvatar = (ImageView)itemView.findViewById(R.id.im_avatar);
            mTvTitle = (TextView)itemView.findViewById(R.id.tv_name);
        }
    }

}
